<?php
$basedir = dirname(__FILE__);

require_once $basedir . '/../classes/minime.php';
require_once $basedir . '/../extlib/Crypt/RSA.php';
require_once $basedir . '/../extlib/Crypt/Random.php';
require_once $basedir . '/../extlib/Crypt/Hash.php';
require_once $basedir . '/../extlib/Math/BigInteger.php';

MiniMe::initialize();

if( count($argv) != 3) {
      echo "\n\tERROR!";
      echo "\n\t\tPlease provide USERNAME JID as parameters to this function.";
      echo "\n\n\tExample:";
      echo "\n\t\t" . 'php subscribeuser.php koski "tkoski@identi.ca"';
      echo "\n\n";
      exit;
}

$subscribeFrom = $argv[1];
$subscribeTo   = $argv[2];

$userLoader = new Minime_User_Loader(Minime::$conf);
$user = $userLoader->getUser($subscribeFrom);

if(!$user) {
    echo "\n\t\tSubscribing user is not local user!";
    echo "\n\n";
    exit;
}


$subsloader = new Minime_User_Subscriptions_Loader(Minime::$conf);
$usersSubscriptions = $subsloader->getSubscriptions($subscribeFrom);

if($usersSubscriptions->getSubscriptionByUri($subscribeTo) !== null) {
    echo "\n\t\tUser '" . $subscribeFrom . "' is already subscribed to '" . $subscribeTo . "'!";
    echo "\n\n";
    exit;
}

echo "\n\tCreating new subscription:";

echo "\n\t\t- Discovering " . $subscribeTo . "'s profile ... (this will take awhile) ...";

$discoverer = new Discovery_Uri($subscribeTo);
$discoverer->__debugoutput = true;
if(!$discoverer->discover()) {
     echo "\n\tDiscovery of '" . $subscribeTo . "' Failed!\n\n";
     exit;
}

echo "\n\t\t- Name : '" . $discoverer->getName() . "'.";
echo "\n\t\t- Atom : '" . $discoverer->getAtomFeedUri() . "'.";
echo "\n\t\t- Hub  : '" . $discoverer->getHubUri() . "'.";

echo "\n\n\tExecuting pubsubhubbub subscription:";

$pubsub = new Pubsubhubbub_Subscribe();
if(!$pubsub->subscribe($subscribeFrom, $discoverer, "Atom feed of user '" . $discoverer->getName() . "'.", Minime::$conf)) {
    echo "\n\tPubsubhubbub Subscription FAILED! (Sorry, log does not yet say why, bad cod here :-( )\n\n";    
    exit;
} else {
    echo "\n\t\t- Pubsubhubbub subscription OK.";    
}

$array['uri'] = $discoverer->getWebfingerProfilePageUri();
$array['name'] = $discoverer->getName();
$array['atomfeed'] = $discoverer->getAtomFeedUri();
$array['alternate'] = $discoverer->getWebfingerProfilePageUri();
$array['logo'] = $discoverer->getLogo();
$usersSubscriptions->addSubscription(Minime_User_Subscription::buildFromArray($array));

$substorer = new Minime_User_Subscriptions_Storer(Minime::$conf);
$substorer->storeSubscription($subscribeFrom, $usersSubscriptions);

echo "\n\n\tExecuting Salmon notice:";
$s = $discoverer->getSalmonUri();
if(!empty($s)) {
    $salmon = new Salmon_Follow();
    if(!$salmon->createFollowAtomAndSendDoEverythingThisIsTemporarTest($user, $discoverer)) {
        echo "\n\tSalmon post FAILED! (Sorry, log does not yet say why, bad cod here :-( )\n\n";    
        exit;
    } else {
        echo "\n\t\t- Salmon notice OK.";    
    }
} else {
    echo "\n\t\t- No Salmon, not sending a salmon notice.";
}

echo "\n\n\tDONE!\n";

